using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;

namespace JNPF.Basics.Logic
{
    public class UserextendBll
    {
        private UserextendIService service = new UserextendService();


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<UserextendEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<UserextendEntity> GetInfoByUserId()
        {
            try
            {
                return await service.GetInfoByUserId(UserProvider.Instance.Get().UserId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<UserextendEntity> GetInfoByUserId(string userId)
        {
            try
            {
                return await service.GetInfoByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建表里面数据
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="comConfig"></param>
        /// <param name="drawProportion"></param>
        /// <returns></returns>
        public async Task<int> CreateUserTend(string userId, string comConfig, decimal? drawProportion)
        {
            try
            {
                UserextendEntity entity = await GetInfoByUserId(userId);
                if (entity != null && entity.Id > 0)
                {
                    entity.ComConfig = comConfig;
                    entity.DrawProportion = drawProportion;
                    return await Update(entity.Id, entity);

                }
                else
                {
                    //如果不是合伙人就不创建
                    entity = new UserextendEntity();
                    entity.CreatorTime = DateTime.Now;
                    entity.UserId = userId;
                    entity.ParentPartnerUserId = "";
                    entity.TotalPointsEarned = 0;
                    entity.WithdrawPoints = 0;
                    entity.TotalPointsSpent = 0;
                    entity.PointsPendingWithdrawal = 0;
                    entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                    entity.CreatorUser = UserProvider.Instance.Get().UserName;
                    entity.DeleteMark = 0;
                    entity.ComConfig = comConfig;
                    entity.DrawProportion = drawProportion;
                    return await Create(entity);
                }
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }





        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<UserextendEntity>> GetPageList(UserextendListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(UserextendEntity entity)
        {
            try
            {
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.CreatorUser = UserProvider.Instance.Get().UserName;
                entity.DeleteMark = 0;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, UserextendEntity entity)
        {
            try
            {
                entity.Id = id;
                if (id < 1)
                {
                    UserextendEntity userextend = await GetInfoByUserId(UserProvider.Instance.Get().UserId);
                    if (userextend != null && userextend.Id > 0)
                    {
                        entity.Id = userextend.Id;
                    }
                }
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(UserextendEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<UserextendEntity>> GetList(UserextendListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}